<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Course as CourseModel;
use app\common\model\course\Category;
use app\common\model\course\Chapter;
use app\common\model\course\Content;
use app\common\model\user\Favorite;
use app\common\model\user\Order;

/**
 * 首页接口
 */
class Course extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $userId = $this->auth->id ?? 0;
        $model = new CourseModel();
        $list = $model->order('updatetime', 'desc')->limit(4)->select();

        if ($userId && $list) {
            $ids = $list->column('id');
            $courseIds = Favorite::whereIn('map_id', $ids)
                ->where('user_id', $userId)->column('map_id');
            $orderCourseIds = Order::whereIn('course_id', $ids)
                ->where('user_id', $userId)
                ->where('status', Order::STATUS_SUCC)
                ->column('course_id');
            foreach ($list as &$value) {
                $value->user_star = (int)in_array($value->id, $courseIds);
                $value->user_buy = (int)in_array($value->id, $orderCourseIds);
            }
            unset($value);
        }
        $this->success('', $list);
    }

    public function category()
    {
        $list = Category::all();
        $this->success('', $list);
    }

    public function list()
    {
        $course_category_id = $this->request->get('course_category_id', '');
        $userId = $this->auth->id ?? 0;
        $model = new CourseModel();
        $list = $model->where(function ($query) use ($course_category_id) {
            if ($course_category_id) {
                $query->where('course_category_id', $course_category_id);
            }
        })->order('updatetime', 'desc')->select();

        if ($userId && $list) {
            $ids = $list->column('id');
            $courseIds = Favorite::whereIn('map_id', $ids)
                ->where('user_id', $userId)->column('map_id');
            $orderCourseIds = Order::whereIn('course_id', $ids)
                ->where('user_id', $userId)
                ->where('status', Order::STATUS_SUCC)
                ->column('course_id');
            foreach ($list as &$value) {
                $value->user_star = (int)in_array($value->id, $courseIds);
                $value->user_buy = (int)in_array($value->id, $orderCourseIds);
            }
            unset($value);
        }
        $this->success('', $list);
    }

    public function info()
    {
        $userId = $this->auth->id ?: 0;
        $id = $this->request->get('id');
        $model = new CourseModel();
        $result = $model->find($id);

        if ($userId) {
            $order = Order::where(['course_id' => $id, 'user_id' => $userId])
                ->where('status', Order::STATUS_SUCC)
                ->find();
            $result->user_buy = $order ? 1 : 0;
            $result->user_star = Favorite::where(['user_id' => $userId, 'map_id' => $id])->find() ? 1 : 0;
        }

        $this->success('', $result ?: (object)[]);
    }

    public function star()
    {
        $userId = $this->auth->id;
        $id = (int)$this->request->post('id', 0);
        if (!$id || !CourseModel::find($id)) {
            $this->error(__('Invalid parameters'));
        }
        $map = Favorite::where('user_id', $userId)->where('map_id', $id)->find();
        if ($map) {
            $map->delete();
        } else {
            Favorite::create([
                'user_id' => $userId,
                'map_id' => $id,
            ]);
        }
        $this->success();
    }

    public function chapter()
    {
        $id = (int)$this->request->get('course_id', 0);
        if (!$id || !CourseModel::find($id)) {
            $this->error(__('Invalid parameters'));
        }
//        $list = Chapter::where('course_id', $id)->order('weigh', 'desc')->select();
//        $this->success('', $list);
        $list = Content::where('course_id', $id)->order(['createtime' => 'asc', 'weigh' => 'desc'])->select();
        $this->success('', $list);
    }

    public function content()
    {
        $id = (int)$this->request->get('chapter_id', 0);
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $list = Content::where('chapter_id', $id)->order('weigh', 'desc')->select();
        $this->success('', $list);
    }
}
